//
//  LKGXNumberModificationView.m
//  LKFXPT
//
//  Created by Ssky on 2016/11/25.
//  Copyright © 2016年 Sky SS. All rights reserved.
//

#import "LKGXNumberModificationView.h"

#define max 1000

@interface LKGXNumberModificationView () <UITextFieldDelegate>

@property (weak, nonatomic) NoHighlightedButton *btnPlus;
@property (weak, nonatomic) NoHighlightedButton *btnMinus;
@property (weak, nonatomic) UITextField *txtCount;
@property (weak, nonatomic) UILabel *lblUnitType;

@property (nonatomic, assign) BOOL isEdit;

@end

@implementation LKGXNumberModificationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(txtCountValueChanged:) name:UITextFieldTextDidChangeNotification object:self.txtCount];
        //
        [self setupUI];
        
        self.txtCount.keyboardType = UIKeyboardTypeNumberPad;
    }
    return self;
}


#pragma mark - methods
///类方法
+ (instancetype)modificationView {
    id obj = [[self alloc] init];
    LKGXNumberModificationView * moView = (LKGXNumberModificationView *)obj;
    
    return moView;
}

- (void)distributeWithDict:(NSDictionary *)dict {
    // @{@"unitType": @"箱", @"unitCount": @(8)}
    self.commodityUnitType = dict[@"unitType"];
    self.commodityCount = [dict[@"unitCount"] intValue];
}


#pragma mark - actions
/// btnPlusClick
- (void)btnPlus:(UIButton *)sender {
//    if (self.commodityCount >= max) {
//        sender.enabled = NO;
//        return;
//    }
    
    self.txtCount.text = [NSString stringWithFormat:@"%d", ++_commodityCount];
    self.btnMinus.enabled = YES;
    // 代理回传
    [self.countDelegate numberModificationView:self commodityCount:self.commodityCount];
}

/// btnMinusClick
- (void)btnMinus:(UIButton *)sender {
    if (self.commodityCount <= 0) {
        sender.enabled = NO;
        return;
    }
    
    self.txtCount.text = [NSString stringWithFormat:@"%d", --_commodityCount];
    self.btnPlus.enabled = YES;
    // 代理回传
    [self.countDelegate numberModificationView:self commodityCount:self.commodityCount];
}

- (void)txtCountValueChanged:(NSNotification *)note {
    _commodityCount = self.txtCount.text.intValue;
    // 代理回传
    if (self.isEdit) {
        [self.countDelegate numberModificationView:self commodityCount:self.commodityCount];
    }
}

#pragma mark - delegates

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.isEdit = YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.isEdit = NO;
}


#pragma mark - setupUI
-(void) setupUI {
    
    
    CGFloat margin = 0;
    
    //
    NoHighlightedButton *btnMinus = [[NoHighlightedButton alloc] init];
    [self addSubview:btnMinus];
    
    [btnMinus setBackgroundImage:[UIImage imageNamed:@"Minus"] forState:UIControlStateNormal];
    [btnMinus addTarget:self action:@selector(btnMinus:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnMinus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(margin);
        make.centerY.equalTo(self);
        make.size.mas_offset(CGSizeMake(20, 20));
    }];
    
    //
    UITextField *txtCount = [[UITextField alloc] init];
    [self addSubview:txtCount];
    
    [txtCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(btnMinus.mas_trailing).offset(margin);
        make.centerY.equalTo(btnMinus);
        make.size.mas_offset(CGSizeMake(40, 20));
    }];
    
    txtCount.borderStyle = UITextBorderStyleBezel;
    txtCount.delegate = self;
    txtCount.font = [UIFont systemFontOfSize:12];
    
    //
    NoHighlightedButton *btnPlus = [[NoHighlightedButton alloc] init];
    [self addSubview:btnPlus];
    
    [btnPlus setBackgroundImage:[UIImage imageNamed:@"Plus"] forState:UIControlStateNormal];
    [btnPlus addTarget:self action:@selector(btnPlus:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnPlus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(txtCount.mas_trailing).offset(margin);
        make.centerY.equalTo(btnMinus);
        make.size.mas_offset(CGSizeMake(20, 20));
    }];
    
    //
    UILabel *lblUnitType = [[UILabel alloc] init];
    [self addSubview:lblUnitType];
    
    lblUnitType.font = [UIFont systemFontOfSize:12];
    
    [lblUnitType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(btnPlus.mas_trailing).offset(margin);
        make.centerY.equalTo(btnMinus);
        make.size.mas_offset(CGSizeMake(20, 20));
    }];
    
    //self
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(100, 22));
    }];
//    self.backgroundColor = [UIColor orangeColor];
    
    //
    self.btnPlus = btnPlus;
    self.btnMinus = btnMinus;
    self.txtCount = txtCount;
    self.lblUnitType = lblUnitType;
}



#pragma mark - setter & getter

- (void)setCommodityCount:(int)commodityCount {
    _commodityCount = commodityCount;
    
    if (commodityCount == 0) {
        self.txtCount.text = @"0";
    }else {
        self.txtCount.text = [NSString stringWithFormat:@"%d", commodityCount];
    }
}

- (void)setCommodityUnitType:(NSString *)commodityUnitType {
    _commodityUnitType = commodityUnitType.copy;
    self.lblUnitType.text = commodityUnitType;
}

- (void)setDataNumModiDict:(NSDictionary<NSString *,id> *)dataNumModiDict {
    _dataNumModiDict = dataNumModiDict;
    
    //distribute
    [self distributeWithDict:dataNumModiDict];
}


- (void)setUnitColor:(UIColor *)unitColor {
    _unitColor = unitColor;
    
    self.lblUnitType.textColor = unitColor;
}





@end


@implementation NoHighlightedButton

- (void)setHighlighted:(BOOL)highlighted {}

@end
